package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.entity.ApplicationReporting;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.enums.SwitchEnum;
import com.evil.common.core.exception.BusinessException;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 应用报表表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface ApplicationReportingMapper extends BaseMapper<ApplicationReporting> {

    Optional<ApplicationReporting> isExistById(Long id);

    default ApplicationReporting findById(Long id) {
        return this.isExistById(id).map(e -> {
            if (SwitchEnum.isOpen(e.getIsDeleted())) {
                throw new BusinessException(RCodeEnum.APPLICATION_REPORTING_DELETED);
            }
            return e;
        }).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_REPORTING_NOT_EXIST));
    }

    Optional<ApplicationReporting> isExistByKey(String reportingKey);

    default ApplicationReporting findByKey(String reportingKey) {
        return this.isExistByKey(reportingKey).map(e -> {
            if (SwitchEnum.isOpen(e.getIsDeleted())) {
                throw new BusinessException(RCodeEnum.APPLICATION_REPORTING_DELETED);
            }
            return e;
        }).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_REPORTING_NOT_EXIST));
    }

    List<ApplicationReporting> findByApplicationId(Long applicationId);

    List<ApplicationReporting> findByAppIds(@Param("applicationIds") Collection<Long> applicationIds);
}